package com.biao.java8.starem_test;

import com.biao.java8.demo.pojo.Employee;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Test03_reduce {
    public static void main(String[] args) {
        List<Employee> listEmployee = new ArrayList<>();
        listEmployee.add(new Employee("abiu", 18, 8000d));
        listEmployee.add(new Employee("二狗", 24, 16000d));
        listEmployee.add(new Employee("黑狗哥", 27, 24000d));

        // stream求和，对 age 和 salary 都求和
        Optional<Employee> optionalEmployee =
                listEmployee.stream().reduce((a, b) -> new Employee("sum", a.getAge() + b.getAge(), a.getSalary() + b.getSalary()));
        System.out.println("stream求和: --------------------------------");
        System.out.println(optionalEmployee.get());

        // 计算 salary 总和
        Double sum = listEmployee.stream().mapToDouble(Employee::getSalary).sum();
        System.out.println(sum);

        // 使用 map 给 abiu 变成大写
        listEmployee.stream().map((a) -> a.getName().toUpperCase()).limit(1).forEach(System.out::println);
    }
}
